using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum FireType
{
    Top,
    Left,
    Right,
    Buttom,
    LeftTop,
    RightTop,
    LeftBottom,
    RightBottom,
}

public class FireObject :MonoBehaviour
{
    private Vector3 Screenpos;
    private Vector3 Shoot;

    public FireType type;

    private float nowcd;
    private float nowdelaytime;
    private int nownum;

    FireInfo fireInfo;

    public BullentInfo nowBullentId;
    public float changeAngle;
    private void Update()
    {
        UpdateChangePosition();

        RestFireInfo();
    }
    private void UpdateChangePosition()
    {
        Screenpos.z = Camera.main.WorldToScreenPoint(PlayerObject.Instance.transform.position).z;
        switch(type)
        {
            case FireType.Top:
                Screenpos.x = Screen.width / 2;
                Screenpos.y = Screen.height;
                Shoot = Vector3.right;
                break; 
                case FireType.Left:
                Screenpos.x = 0;
                Screenpos.y = Screen.height/2;
                Shoot = Vector3.right;

                break;
                case FireType.Right:
                Screenpos.x = Screen.width ;
                Screenpos.y = Screen.height / 2;
                Shoot = Vector3.left;
                break;
            case FireType.Buttom:
                Screenpos.x = Screen.width/2;
                Screenpos.y = 0;
                Shoot = Vector3.right;
                break;
            case FireType.LeftTop:
                Screenpos.x = 0;
                Screenpos.y = Screen.height ;
                Shoot = Vector3.right;
                break;
            case FireType.RightTop:
                Screenpos.x = Screen.width;
                Screenpos.y = Screen.height ;
                Shoot = Vector3.left;
                break;
            case FireType.LeftBottom:
                Screenpos.x = 0;
                Screenpos.y = 0;
                Shoot = Vector3.right;
                break;
            case FireType.RightBottom:
                Screenpos.x = Screen.width;
                Screenpos.y = 0;
                Shoot = Vector3.left;
                break;
        }
    }

    private void RestFireInfo()
    {
        if (nownum != 0 && nowcd != 0)
        {
            return;
        }
        if (fireInfo != null)
        {
            nowdelaytime -= Time.deltaTime;
            if (nowdelaytime > 0)
            {
                return;
            }
        }
        List<FireInfo> list = GameDataMgr.Instance.firedata.fireInfos;
         fireInfo = list[Random.Range(0,list.Count)];
        nowcd = fireInfo.cd;
        nowdelaytime=fireInfo.Delaytime;
        nownum = fireInfo.num;
        string[] str=fireInfo.name.Split(',');
        int beginnum=int.Parse(str[0]);
        int endnum=int.Parse(str[1]);
        int randomId = Random.Range(beginnum, endnum + 1);
        nowBullentId = GameDataMgr.Instance.bullentData.bullentInfoList[randomId - 1];
        if(fireInfo.type==2)
        {


            switch(type)
            {
                case FireType.RightBottom:
                case FireType.LeftBottom:
                    case FireType.RightTop:
                    case FireType.LeftTop:
                    changeAngle = 90f / (nownum + 1);
                    break;
                case FireType.Top:
                case FireType.Buttom:
                case FireType.Right:
                case FireType.Left:
                    changeAngle = 180f / (nownum + 1);
                    break;
            }
        }

    }

    private void UpdateFire()
    {
        if (nowcd == 0 && nownum == 0)
        {
            return;
        }
        nowcd-=Time.deltaTime;
        if (nowcd > 0)
        {
            return;
        }
        GameObject bullent;
        BullentObject bullentObject;
        Vector3 nowDir;
      switch(fireInfo.type)
        {
            case 1:
                bullent = Instantiate(Resources.Load<GameObject>(nowBullentId.resName));
                bullentObject = bullent.AddComponent<BullentObject>();
                bullentObject.IntInfo(nowBullentId);

                bullentObject.transform.position=this.transform.position;
                bullentObject.transform.rotation = Quaternion.LookRotation(PlayerObject.Instance.transform.position
                    -this.transform.position );
                nownum--;
                nowcd=nownum==0? 0 : fireInfo.cd;
                break;

                case 2:
                if (nowcd == 0)
                {
                    for(int i=0;i<nownum;i++)
                    {
                        bullent = Instantiate(Resources.Load<GameObject>(nowBullentId.resName));
                        bullentObject = bullent.AddComponent<BullentObject>();
                        bullentObject.IntInfo(nowBullentId);

                        bullentObject.transform.position = this.transform.position;
                        nowDir = Quaternion.AngleAxis(changeAngle*i,Vector3.up)*Shoot;
                        bullentObject.transform.rotation = Quaternion.LookRotation(nowDir);
                    }
                    nownum = 0; nowcd = 0;
                }
                else
                {
                    bullent = Instantiate(Resources.Load<GameObject>(nowBullentId.resName));
                    bullentObject = bullent.AddComponent<BullentObject>();
                    bullentObject.IntInfo(nowBullentId);

                    bullentObject.transform.position = this.transform.position;
                    nowDir = Quaternion.AngleAxis(changeAngle * (fireInfo.num-nownum), Vector3.up) * Shoot;
                    bullentObject.transform.rotation = Quaternion.LookRotation(nowDir);

                    nownum--;
                    nowcd = nownum == 0 ? 0 : fireInfo.cd;
                }
                    break;
        }
    }
}
